package com.note.util;

import org.apache.tomcat.util.codec.binary.Base64;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * Created by ADMIN on 2018/3/15.
 */
public class NoteUtil {
    /**
     * 生成ID
     * @return
     */
    public static String createId(){
        UUID uuid = UUID.randomUUID();
        return uuid.toString();
    }


    /**
     * 密码加密处理
     * @param msg 明文
     * @return  加密之后的密文
     */
    public static String md5(String msg){
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] input = msg.getBytes();//将字符串变成字节数组
            byte[] output = md.digest(input);//此方法处理字节数组，所以要把传入的字符串处理成字节数组
            //将md5处理后的output结果转成字符串
            //String str = new String(output);会出现乱码
            //用Base64算法转成字符串,添加一个jar包，利用它的API
            String str = Base64.encodeBase64String(output);
            return str;
        } catch (NoSuchAlgorithmException e) {
            System.out.println("加载密码失败");
            return "";
        }
    }

    public static void main(String[] args){
        System.out.println(createId());
        System.out.println(createId());
        System.out.println(md5("123"));
    }
}
